package com.ihrm.company.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.CompanyService;
import com.ihrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小伟
 * @version V1.0
 * @Package com.ihrm.company.controller
 * @date 2020/12/10 10:50
 * @Copyright © 2020-2021 郑州恒星科技有限责任公司
 */
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    /**
     * 新增企业信息
     *
     * @param company 封装企业对象的信息
     * @return 结果
     */
    @PostMapping("")
    public Result addCompany(@RequestBody Company company) {
        //业务操作
        companyService.addCompany(company);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id修改公司信息
     *
     * @param id
     * @param company
     * @return
     */
    @PutMapping("/{id}")
    public Result updateCompany(@PathVariable("id") String id, @RequestBody Company company) {
        company.setId(id);
        companyService.updateCompany(company);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id删除企业信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteCompanyById(@PathVariable("id") String id) {
        companyService.deleteCompanyById(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id查询企业信息
     *
     * @param id 企业id
     * @return
     */
    @GetMapping("/{id}")
    public Result getCompanyById(@PathVariable("id") String id) {
        Company company = companyService.getCompanyById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(company);
        return result;
    }

    @GetMapping("")
    @CrossOrigin
    public Result getAllCompanies() {
        List<Company> companyList = companyService.getCompanies();
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(companyList);
        return result;
    }
}
